package com.example.myapplication.ui.movie

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.BuyTicketActivity
import com.example.myapplication.R

class TimeSlotAdapter (val timeSlotList:List<TimeSlot_info>, val context: Context):
    RecyclerView.Adapter<TimeSlotAdapter.ViewHolder>(){

    inner class ViewHolder(view:View):RecyclerView.ViewHolder(view){
        val startTime:TextView = view.findViewById(R.id.startTime)
        val endTime:TextView = view.findViewById(R.id.endTime)
        val language:TextView = view.findViewById(R.id.language)
        val hall:TextView = view.findViewById(R.id.hall)
        val price:TextView = view.findViewById(R.id.price)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.timeslot_info, parent, false)
        val viewHolder = ViewHolder(view)

        view.setOnClickListener {
            val position = viewHolder.adapterPosition

            val mIntent = Intent(context, BuyTicketActivity::class.java)
            mIntent.putExtra("cinema_name", timeSlotList[position].cinema_name)
            mIntent.putExtra("movie_name", timeSlotList[position].movie_name)
            mIntent.putExtra("date", timeSlotList[position].date)
            mIntent.putExtra("start_time", timeSlotList[position].startTime)
            mIntent.putExtra("end_time", timeSlotList[position].endTime)
            mIntent.putExtra("language", timeSlotList[position].language)
            mIntent.putExtra("price", timeSlotList[position].price)
            mIntent.putExtra("hall", timeSlotList[position].hall)
            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK )
            context.startActivity(mIntent)
        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val startTime = timeSlotList[position].startTime
        val endTime = timeSlotList[position].endTime
        val language = timeSlotList[position].language
        val hall = timeSlotList[position].hall
        val price = timeSlotList[position].price

        holder.startTime.text = startTime
        holder.endTime.text = "${endTime}散场"
        holder.language.text = language
        holder.hall.text = hall
        holder.price.text = "${price}元"
    }

    override fun getItemCount(): Int {
        return timeSlotList.size
    }
}